package xjtu.sad.zbsj.task.annotater.labelTask;

import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.*;
import xjtu.sad.zbsj.common.PageParam;
import xjtu.sad.zbsj.common.Result;
import xjtu.sad.zbsj.dao.labelTask.LabelTaskDao;
import xjtu.sad.zbsj.dao.template.TemplateDao;
import xjtu.sad.zbsj.template.UserInfo;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/task/annotater/labelTask")
public class LabelTaskAPI {

    @Resource
    private LabelTaskService labelService;

    @Resource
    private LabelTaskDao labelTaskDao;

    @Resource
    private TemplateDao templateDao;

    /**
     * 获取当前用户所有领取的任务的信息
     *
     * @param userId    用户id
     * @param pageParam 分页参数
     * @return 查询到的该用户所有领取的任务信息
     */
    @PostMapping("/getDrawInfo")
    public Result getDrawInfo(@RequestParam Integer userId, @RequestBody PageParam pageParam) {
        UserInfo userInfo = templateDao.getUserInfo(userId);// 获得当前角色的信息
        if (userInfo != null) {
            // 找到该角色信息
            Page<DrawInfo> drawInfoList = labelService.getDrawInfo(userId, pageParam);
            return Result.success(drawInfoList);
        } else {
            // 未找到该角色信息
            return Result.fail(Result.ERR_CODE_UNLOGIN, "未找到当前角色信息,请登录后重试");
        }
    }

    /**
     * 获得某个任务的详细信息
     *
     * @param taskId 任务id
     * @return 该任务的详细信息
     */
    @GetMapping("/getTaskInfo")
    public Result getTaskInfo(@RequestParam Integer taskId) {
        TaskInfo taskInfo = labelService.getTaskInfo(taskId);
        if (taskInfo != null) {
            return Result.success(taskInfo);
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "未找到当前任务的相信信息,请确认后重试");
        }
    }

    /**
     * 获得标注详情
     *
     * @param drawId 领取id
     * @return 该领取对应的模板信息, 任务信息, 已标注样本及未标注样本信息, 当前第一个未被标注的样本下标
     */
    @GetMapping("/getDrawDetail")
    public Result getDrawDetail(@RequestParam Integer drawId) {
        boolean drawInfoExist = labelTaskDao.judgeDrawInfoExist(drawId);
        if (drawInfoExist) {
            DrawDetail drawDetail = labelService.getDrawDetail(drawId);
            if (drawDetail != null) {
                return Result.success(drawDetail);
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "获取信息失败");
            }
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "未找到当前领取的详细信息,请确认后重试");
        }
    }

    /**
     * 修改标注结果
     *
     * @param markResult 标注结果列表
     * @return 修改结果
     */
    @PostMapping("/changeMarkResult")
    public Result changeMarkResult(@RequestBody MarkResult markResult) {
        boolean drawInfoExist = labelTaskDao.judgeDrawInfoExist(markResult.getDrawId());
        if (drawInfoExist) {
            boolean success = labelService.changeMarkResult(markResult);
            if (success) {
                return Result.success("修改成功");
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "修改失败,请重试");
            }
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "领取记录不存在,请确认无误后重试");
        }
    }

    /**
     * 设置任务领取状态为已完成,同时需要检查当前任务是否都已标注完成,如果完成,设置任务状态为未审核
     *
     * @param drawId 领取记录id
     * @return 设置结果
     */
    @PostMapping("/setDrawFinish")
    public Result setDrawFinish(@RequestParam Integer drawId) {
        DrawInfo drawInfo = labelTaskDao.getDrawInfo(drawId);// 获得当前领取的信息
        if (drawInfo == null) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "当前领取记录不存在,请重试");
        } else {
            // 判断是否所有标注都已经完成
            List<MarkInfo> markInfoList = labelTaskDao.getMarkInfoList(drawId);
            boolean flag = true;
            for (MarkInfo markInfo : markInfoList) {
                if (!"DYS".equals(markInfo.getStatus())) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                // 如果完成的话设置领取状态为YWC,同时设置完成时间时间
                labelTaskDao.setDrawFinish(drawId);

                // 判断该任务下所有任务是否都已经完成,如果完成则设置任务状态为已完成,等待审核
                labelService.setTaskStatus(drawInfo.getTaskId());
                return Result.success("修改成功");
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "当前任务未标注完成");
            }

        }
    }

}
